package com.mairuide.robot;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mairuide._frame.base.BaseRepository;
import com.mairuide._frame.base.BaseServiceProxy;
import com.mairuide.menuinfo.MenuInfo;
import com.mairuide.menuinfo.MenuInfoService;

@Service
@Transactional(readOnly = true)
public class RobotRulesService  extends BaseServiceProxy<RobotRules> {
	
	@Resource
	private RobotRulesRepository robotRulesRepository;
	
	@Resource
	private MenuInfoService menuInfoService;
	
	@Override
	protected BaseRepository<RobotRules> getBaseRepository() {
		return robotRulesRepository;
	}
	
	/**
	 * 根据案件类型请求url查询机器人返回菜单
	 * @param caseType
	 * @param userId
	 * @param requestUrl
	 * @return
	 */
	public List<MenuInfo> getMenuInfo4CaseIdAndUserId(String caseType,Integer userId,String moduleCode,String pModuleCode){
		List<MenuInfo> result = new ArrayList<MenuInfo>();
		List<MenuInfo> mfs = menuInfoService.findByUserId(userId);
		List<RobotRules> rrs = null;
		if(mfs != null  && mfs.size() > 0) {
			rrs = robotRulesRepository.findByCaseTypeAndModuleCodeAndPModuleCode(caseType, moduleCode,pModuleCode);
			if(rrs == null  || rrs.isEmpty()) {
				/** 如果没有能查询出菜单信息则查询默认菜单提示 **/
				rrs = robotRulesRepository.findByCaseTypeAndModuleCodeAndPModuleCode("NOT_MENU_INFO", moduleCode,pModuleCode);
			}
			if(rrs != null && rrs.size() > 0) {
				for(RobotRules rr : rrs) {
					String[] menuIds = rr.getMenuId().split(",");
					for(String mId : menuIds) {
						for(MenuInfo mi : mfs) {
							if(mId.equals(mi.getMenuId().toString())) {
								result.add(mi);
							}
						}
					}
				}
			}
		}
		return result;
		
	}

}
